﻿using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ScreenMonitor.Common;
using System.Collections.Generic;

namespace ScreenMonitor.ServerLib
{
    /// <summary>
    /// 为客户端提供统一管理
    /// </summary>
    public static class ClientManager
    {

        /// <summary>
        /// 客户端列表实例
        /// </summary>
        private static ArrayList Clients = ArrayList.Synchronized(new ArrayList());
        private static object lockObj = new object();

        #region========公有委托========

        public delegate void ClientBeforeOperateEventHandler(ClientManagerOperateEventArgs e);
        public delegate void ClientAfterOperateEventHandler(ClientManagerOperateEventArgs e);

        #endregion

        #region==========事件==========

        public static event ClientBeforeOperateEventHandler BeforeOperateClient;
        public static event ClientAfterOperateEventHandler AfterOperateClient;

        #endregion

        #region========公有方法========

        /// <summary>
        /// 添加客户端到客户端列表中
        /// </summary>
        /// <param name="Client"></param>
        public static void AddClient(ClientObject Client)
        {
            lock (lockObj)
            {
                ClientManagerOperateEventArgs e = new ClientManagerOperateEventArgs(ClientManagerOperateType.Add, Client);
                
                if (BeforeOperateClient != null)
                {
                    BeforeOperateClient(e);
                }
                if (!e.IsCanceled)
                {
                    Clients.Add(Client);
                }
                if (AfterOperateClient != null)
                {
                    AfterOperateClient(e);
                }
            }

        }

        /// <summary>
        /// 从客户端列表中移除指定客户端
        /// </summary>
        /// <param name="Client"></param>
        public static void RemoveClient(ClientObject Client)
        {
            lock (lockObj)
            {
                ClientManagerOperateEventArgs e = new ClientManagerOperateEventArgs(ClientManagerOperateType.Remove, Client);
                if (BeforeOperateClient != null)
                {
                    BeforeOperateClient(e);
                }
                if (!e.IsCanceled)
                {
                    Clients.Remove(Client);
                }
                if (AfterOperateClient != null)
                {
                    AfterOperateClient(e);
                }
            }
        }

        /// <summary>
        /// 获取目前所有客户端的实例
        /// </summary>
        /// <returns></returns>
        public static ClientObject[] GetAllClient()
        {
            return Clients.Cast<ClientObject>().ToArray();
        }

        /// <summary>
        /// 获取目前为指定状态的客户端列表
        /// </summary>
        /// <param name="State"></param>
        /// <returns></returns>
        public static List<ClientObject> GetAllClient(ClientState State)
        {
            List<ClientObject> list = new List<ClientObject>();
            foreach(ClientObject obj in GetAllClient())
            {
                if(obj.State==State)
                {
                    list.Add(obj);
                }
            }
            return list;
        }

        /// <summary>
        /// 获取目前为指定签到状态的客户端列表
        /// </summary>
        /// <param name="SignState"></param>
        /// <returns></returns>
        public static List<ClientObject> GetAllClient(SignState SignState)
        {
            List<ClientObject> list = new List<ClientObject>();
            foreach (ClientObject obj in GetAllClient())
            {
                if (obj.SignState == SignState)
                {
                    list.Add(obj);
                }
            }
            return list;
        }

        #endregion

    }
}
